package org.example.web;

import org.example.vo.LoginProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/6/7
 * Email haijunzhou@hnit.edu.cn
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    LoginProperties loginProperties;
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object user = session.getAttribute(loginProperties.getSessionKey());
        Cookie[] cookies = request.getCookies();
        String userId = null;
        String timeType=null;
        if(cookies!=null){
            for (Cookie cookie : cookies) {
                if("KLGHLJNHL".equals(cookie.getName())){
                    userId = cookie.getValue();
                }
                if("UIANT".equals(cookie.getName())){
                    timeType=cookie.getValue();
                    System.out.println("timeType = " + timeType);
                }
            }
        }

        if(user == null && userId!=null && timeType!=null){
            // 自动登录
            String sql = "select * from %s where id=?";
            sql = String.format(sql, loginProperties.getUserTable());
            user = jdbcTemplate.queryForObject(sql,
                    new BeanPropertyRowMapper<>(loginProperties.getUserClassObject()),
                    userId);
            session.setAttribute(loginProperties.getSessionKey(),user);

            //如果是自动登录的话那么 选择自动登录时 选择的时间也要刷新
            switch(timeType){
                case("day"):
                    System.out.println("day+Inteceptor");
                    reSetCookie(response,"KLGHLJNHL",userId,(24*60*60));
                    reSetCookie(response,"UIANT",timeType,(24*60*60));
                    break;
                case "week":
                    System.out.println("week+Inteceptor");
                    reSetCookie(response,"KLGHLJNHL",userId,(7*24*60*60));
                    reSetCookie(response,"UIANT",timeType,(7*24*60*60));
                    break;
                case "month":
                    System.out.println("month+Inteceptor");
                    reSetCookie(response,"KLGHLJNHL",userId,(31*24*60*60));
                    reSetCookie(response,"UIANT",timeType,(31*24*60*60));
                    break;
            }

            return true;
        }

        // TODO 权限访问控制  => 添加车辆
        return true;
    }

    private void reSetCookie(HttpServletResponse response,String key,String value,int time){
        Cookie cookie=new Cookie(key,value);
        //设置cookie的作用路径  这里是根目录 代表所有的路径都可以使用      在别的路径设置的 他的父路径访问不到
        cookie.setPath("/");
        cookie.setMaxAge(time);
        response.addCookie(cookie);
    }
}
